<?php
  
function saveResume($mname,$applicant_id,$fname,$lname,$gender,$a_address,$city,$country,$telephone,$mobile,$bdate,$pob,$civilstatus,$nationality,$height,$weight,$religion,$noofchildren,$fathersname,$mothersname){
    $date = date('Y-m-d h:i:s');
    if($civilstatus == 'Civil Status'){
      $civilstatus = '';
    }
    $weight = (is_numeric($weight) ? $weight : 0);
    $height = (is_numeric($height) ? $height : 0);
    $noofchildren = (is_numeric($noofchildren) ? $noofchildren : 0);
    
    $phql = "UPDATE applicant 
             SET mname='{$mname}',fname = '{$fname}',lname='{$lname}',gender='{$gender}',tel='{$telephone}',
                 mobile='{$mobile}',dob='{$bdate}',pob='{$pob}',civilstatus='{$civilstatus}',
                 nationality='{$nationality}',height={$height},weight={$weight},religion='{$religion}',
                 children={$noofchildren},father='{$fathersname}',mother='{$mothersname}',Resume_Updated = '{$date}' WHERE applicant_id = '{$applicant_id}'
                 ";
    $result1 = mysql_query($phql);

    if(!$result1){
      return mysql_error();
      exit();
      break;

    }
    //CHECK IF ADDRESS IS EXISTING
    $phql2 = "SELECT * FROM a_address where applicant_id = {$applicant_id}";
    $result = mysql_num_rows(mysql_query($phql2));
    if($result){
        $result3 = updateAddress($applicant_id,$a_address,$city,$country);
    }else{
        $result3 = insertAddress($applicant_id,$a_address,$city,$country);
    }
    
    return $result1;
}
function updateAddress($applicant_id,$a_address,$city,$country){
    $phql = "UPDATE a_address SET a_address = '{$a_address}',city='{$city}',country = '{$country}' 
             WHERE applicant_id = {$applicant_id}";
    return mysql_query($phql);
}
function insertAddress($applicant_id,$a_address,$city,$country){
    $phql = "INSERT INTO a_address(applicant_id,a_address,city,country) 
             VALUES({$applicant_id},'{$a_address}','{$city}','{$country}')";
    return mysql_query($phql);

}

function apply($job_id,$applicant_id,$status)
{
  //date_timezone_set('Asia/Hongkong');
  $date = date('Y-m-d');
  $phql = "INSERT INTO job_apply_applicant(Job_Id,Applicant_ID,Date_apply,Status) VALUES('{$job_id}','{$applicant_id}',NOW(),'{$status}')";
  $result = mysql_query($phql);


  $phql2 = "SELECT * from job_order jo left join employer e on e.employer_id = jo.employer_id";
  $row = mysql_fetch_array(mysql_query($phql2));

  $fullname = $_SESSION['fullname'];
  $message = $fullname.' has apply in your job posting: '.$row['Title'];
  $href ='';
  insertNotif3($row['employer_id'],"applicant_application",$message,$href);
  
  if($result)
  {
     return true;
  }
  else
  {
     return false;
  }
}


function listApplicantsJobRegistration()
{

	$phql = "SELECT jaa.applicant_id,jaa.Job_id as registration_id,e.ename,CONCAT(a.fname,' ',a.lname) as fullname,
		a.email,jo.Title,jaa.status, jo.refid 
		FROM job_apply_applicant jaa 
		LEFT JOIN job_order jo ON jo.job_id = jaa.Job_id
		LEFT JOIN employer e ON e.employer_id = jo.employer_id
		LEFT JOIN applicant a ON jaa.applicant_id = a.applicant_id";
	
	$data = mysql_query($phql);

	return $data;	
}

function listApplicantAccounts($start,$limit,$order_by)
{
  $phql = "SELECT ap.email,ap.fname,ap.lname,ap.mname,a.user,a.account_id,ap.applicant_id
      FROM accounts a
      LEFT JOIN applicant ap ON a.emailadd = ap.email
      WHERE a.level_no = 3 and ap.a_status != 4
      $order_by
      LIMIT $start,$limit
      ";
  $data = mysql_query($phql);
  return $data;
}

function getTotalApplicantPages()
{
	$phql = "SELECT COUNT(*) as num
      FROM accounts a
      LEFT JOIN applicant ap ON a.emailadd = ap.email
      WHERE a.level_no = 3 and ap.a_status != 4";

    $total_pages = mysql_fetch_array(mysql_query($phql));

    return $total_pages['num'];
}


function insertNotif3($receiver_id,$type,$message,$href)
{
  $phql = "INSERT INTO employer_notif(receiver_id,type,message,href) 
       VALUES({$receiver_id},'{$type}','{$message}','{$href}')";
  

  $result = mysql_query($phql);
  
  $message = array(
      "message" => $message,
      "receiver_id" => $receiver_id,
      "notif_id" => mysql_insert_id(),
      "date_created" => CURR_DATE
    );
  $pusher = new Pusher(PUSHER_APP_KEY, PUSHER_APP_SECRET, PUSHER_APP_ID);
  $pusher->trigger('my-channel', 'my-event2', $message);
  return $result;
}
?>